package com.an.service;

import com.an.dao.LoginLogDao;
import com.an.dao.UserDao;
import com.an.domain.LoginLog;
import com.an.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginLogDao loginLogDao;

    public boolean hasMatchUser(String userName,String password){
        int match = userDao.getMatchCount(userName,password);
        return match > 0;
    }

    public User findUserByUserName(String userName){
        return userDao.findUserByUserName(userName);
    }

    @Transactional//目的在于开启事务
    public void loginSuccess(User user){
        user.setCredits(user.getCredits() + 5);
        LoginLog loginLog = new LoginLog();
        loginLog.setIp(user.getLastIp());
        loginLog.setLoginDate(user.getLastVisit());
        loginLog.setUserId(user.getUserId());
        userDao.updateLoginInfo(user);
        loginLogDao.insertIntoLoginLog(loginLog);
    }
}
